package wsa.request;

import javax.annotation.Resource;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.Addressing;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebService
(
   portName = "AddressingServicePort",
   serviceName = "AddressingService",
//   wsdlLocation = "WEB-INF/wsdl/AddressingService.wsdl", //do not provide the wsdl and let the endpoint publish the proper one with addressing policy
   targetNamespace = "http://www.jboss.org/jbossws/ws-extensions/wsaddressing",
   endpointInterface = "wsa.request.ServiceIface"
)
@Addressing(enabled=true, required=true)
@HandlerChain(file = "serverchain.xml")
public class ServiceImpl implements ServiceIface
{
   private Logger log = LoggerFactory.getLogger(this.getClass());
   
   @Resource
   private WebServiceContext context;
   
   public String sendMyMsg(String myMsg)
   {
      if ("Sleepy".equals(myMsg))
      {
         try
         {
            log.info("Sleeping...");
            Thread.sleep(30 * 1000);
            log.info("...end of sleeping.");
            MessageContext messageContext = context.getMessageContext();
        	System.out.println("Received message ID: " + messageContext.get("MessageID"));
        	System.out.println("Reply To address: " + messageContext.get("ReplyTo"));
        	System.out.println("Fin de la invocacion -----------------");
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }
      }
      return "Envio el mensaje:  " + myMsg + "!";
   }
}
